//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.base.OrderSet;
import com.aidong.dao.wills.BaseDao;
import com.aidong.model.MenuAuthority;
import com.aidong.model.wills.SysMenu;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

@Repository
public class SysMenuDao extends BaseDao<SysMenu, Integer> {
	public SysMenuDao() {
	}

	public List<SysMenu> getMenuListByParent(int parentId) {
		return this.queryList("parentId=:p1", new Object[]{Integer.valueOf(parentId)}, (new OrderSet()).asc("sort"), -1);
	}

	public MenuAuthority getMenuAuthorityById(int menuId) {
		String sql = "select m.*, 0 authority from SysMenu m where MenuId = :p1";
		return (MenuAuthority)this.getBySql(MenuAuthority.class, sql, new Object[]{Integer.valueOf(menuId)});
	}

	public List<MenuAuthority> getMenuAuthorityList() {
		String sql = "select m.*, 0 authority from SysMenu m order by sort";
		return this.queryListBySql(MenuAuthority.class, sql, new Object[0]);
	}

	public List<SysMenu> getShortcutMenuList() {
		return this.queryList("shortcut>0", (Object[])null, (new OrderSet()).asc("shortcut"), -1);
	}

	public List<MenuAuthority> getUserMenuAuthorityList(String userCode) {
		return this.getMenuAuthorityList(userCode, 1);
	}

	public List<MenuAuthority> getGroupMenuAuthorityList(String groupCode) {
		return this.getMenuAuthorityList(groupCode, 2);
	}

	public List<MenuAuthority> getMenuAuthorityList(String codeId, int type) {
		String sql = "select m.*, isnull(authority,0) authority from SysMenu m\nleft join SysMenuAuthority a on m.MenuId=a.MenuId and CodeId = :p1 and Type = :p2\norder by sort";
		return this.queryListBySql(MenuAuthority.class, sql, new Object[]{codeId, Integer.valueOf(type)});
	}

	public List<MenuAuthority> getUserAuthorityMenuList(String userCode) {
		return this.getAuthorityMenuList(userCode, 1);
	}

	public List<MenuAuthority> getGroupAuthorityMenuList(String groupCode) {
		return this.getAuthorityMenuList(groupCode, 2);
	}

	public List<MenuAuthority> getAuthorityMenuList(String codeId, int type) {
		String sql = "select m.*, authority from SysMenu m inner join SysMenuAuthority a on m.MenuId=a.MenuId\nwhere CodeId = :p1 and Type = :p2 order by sort";
		return this.queryListBySql(MenuAuthority.class, sql, new Object[]{codeId, Integer.valueOf(type)});
	}

	public void updateUserAuthority(String userCode, Map<Integer, Integer> authMap) {
		this.updateAuthority(userCode, 1, authMap);
	}

	public void updateGroupAuthority(String groupCode, Map<Integer, Integer> authMap) {
		this.updateAuthority(groupCode, 2, authMap);
	}

	public void updateAuthority(String codeId, int type, Map<Integer, Integer> authMap) {
		Session session = this.getSession();
		SQLQuery query = session.createSQLQuery("delete from SysMenuAuthority where CodeId = :p1 and Type = :p2");
		query.setString("p1", codeId);
		query.setInteger("p2", type);
		query.executeUpdate();
		query = session.createSQLQuery("insert into SysMenuAuthority (CodeId, Type, MenuId, Authority) values (:p1, :p2, :p3, :p4)");
		query.setString("p1", codeId);
		query.setInteger("p2", type);
		Iterator i$ = authMap.keySet().iterator();

		while(i$.hasNext()) {
			Integer menuId = (Integer)i$.next();
			query.setInteger("p3", menuId.intValue());
			query.setInteger("p4", ((Integer)authMap.get(menuId)).intValue());
			query.executeUpdate();
		}

	}
}
